<?php
/*
 * Author : QuangTK
 * Date : 04-12-2012
 * */

class It_Model_History extends Zend_Db_Table_Abstract
{
    protected $_name = 'history';

    /*******INSERT HISTORY *******/
    public function addHistory($history)
    {
		$this->insert( $history );
    }

    /*******GET History *******/
    public function getsHistory($option)
    {
		$page = $option['page'];
		$count = $option['count'];
        $select = $this->select()->from('history as h')
                                //->order('date DESC')
								//->joinLeft(array('i'=>'items'), 'i.equipmentId = h.equipmentId' , array('i.itemName', 'i.priceItem'))
								->limitPage($page, $count)	
                                ->order('historyId DESC')							
                                ->setIntegrityCheck( false );								
        $results = $this->fetchAll($select);			
        if ( !$results )
        {
            throw new Exception( 'Could not query : {}');
        }
        return $results;
    }
	
	/*******GET History *******/
    public function searchHistory($startDate, $endDate, $sltFill_add, $sltFill_project, $sltFill_category, $option)
    {					
        $select = $this->select()->from('history as h', array('equipmentName', 'equipmentId', 'action', 'username', 'equipmentField', 'valueOld', 'valueNew', 'date'))                      
								->joinLeft(array('i'=>'items'), 'i.equipmentId = h.equipmentId' , array('i.itemName', 'i.priceItem'))
								->where('date > ?', date("Y-m-d", $startDate))
								->where('date <= ?', date("Y-m-d", $endDate))
                                ->order('historyId DESC')								
                                ->setIntegrityCheck( false );
		if(!isset($option['excel'])){
    		$page = $option['page'];
    		$count = $option['count'];	
			$select->limitPage($page, $count);
		}
	    $str = '';
        if($sltFill_add == 1){
            $str .= "(action = 'add' OR action = 'add item')";
        }
        if($sltFill_project == 1){
            if($str)
                $str .= ' OR ';
            $str .= "equipmentField Like '%projectID%'";
        }
        if($sltFill_category == 1){
            if($str)
                $str .= ' OR ';
            $str .= "equipmentField Like '%categoryId%'";
        }
        if($str)
		  $select->where("($str)");
          
        $results = $this->fetchAll($select);
        if ( !$results )
        {
            throw new Exception( 'Could not query : {}');
        }
        return $results;
    }
    public function countHistory($startDate, $endDate, $sltFill_add, $sltFill_project, $sltFill_category)
    {					
        $select = $this->select()->from('history as h', array('count(*) as Total'))                     
								->joinLeft(array('i'=>'items'), 'i.equipmentId = h.equipmentId' , array())
								->where('date > ?', date("Y-m-d", $startDate))
								->where('date <= ?', date("Y-m-d", $endDate))								
                                ->setIntegrityCheck( false );
		
	    $str = '';
        if($sltFill_add == 1){
            $str .= "(action = 'add' OR action = 'add item')";
        }
        if($sltFill_project == 1){
            if($str)
                $str .= ' OR ';
            $str .= "equipmentField Like '%projectID%'";
        }
        if($sltFill_category == 1){
            if($str)
                $str .= ' OR ';
            $str .= "equipmentField Like '%categoryId%'";
        }
        if($str)
		  $select->where("($str)");
		
        $results = $this->fetchRow($select);
        if ( !$results )
        {
            throw new Exception( 'Could not query : {}');
        }
        return $results;
    }

	
	
    /*******COUNT ITEM*******/
    public function countItems()
    {
        $query = $this->select()
                ->from('history as h','count(*) as total');
        $result = $this->fetchRow($query);
        return $result->total;
    }

}